home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!ix-hou7-11
- From: Jackson7@ix.netcom.com (T. Hubbard)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer-to-Double as Function Arg
- Date: 16 Jan 1996 07:15:45 GMT
- Organization: Netcom
- Message-ID: <4dfjb1$pln@ixnews5.ix.netcom.com>
- References: <4dfccl$j5h@colossus.holonet.net>
- NNTP-Posting-Host: ix-hou7-11.ix.netcom.com
- X-NETCOM-Date: Mon Jan 15 11:15:45 PM PST 1996
- Summary: pointer to pointer example.
- Keywords: pointers, **
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4dfccl$j5h@colossus.holonet.net>,
- mitch@news.mdli.com (Mitch Miller) wrote:
- >I'm having a problem with a function that takes a
- >couple of pointer-to-double arguments, initializes then
- >and assigns values to them in an array style:
- >
- >Function is something like:
- >
- > int GetValue( long iDim, double * Values1, double *
- > Values2 )
- > {
- > int iter;
- > Values1 = (double *) malloc( iDim * sizeof( double ));
- > if (Values1 == NULL )
- > ....
- > Values2 = (double *) malloc( iDim * sizeof( double ));
- > if (Values2 == NULL )
- > ...
- >
- > for (iter=1; iter<iDim; iter++)
- > {
- > Values1[iter] = ...;
- > Values2[iter] = ...;
- >
- > }
- >
- > return 1;
- > }
- >
- >Call from main:
- >
- > double * Ptr1;
- > double * Ptr2;
- >
- > if (!GetValue( iSize, Ptr1, Ptr2 ) )....
- >
- >
- >When I look at the values of Values1 and Values2 in GetValue, they have
- >the correct values.
- >
- >However, back in main, they have NULL values.
- >
- >If I make Ptr1 and Ptr2 global variables so that they are not included
- >in the function calls, the code works fine.
- >
- >I've checked the FAQs, but couldn't find anything quite like this.
- >
- >Thanks in advance...
-
- Hope this helps.....
-
- You may want to try using a pointer to a pointer. This will avoid that nasty
- pass by reference problem you have been experiencing.
-
- int GetValue( long iDim, double **Values1, double **Values2 )
- {
- int iter;
- *Values1 = (double *) malloc( iDim * sizeof( double ));
- if (*Values1 == NULL )
- ....
- *Values2 = (double *) malloc( iDim * sizeof( double ));
- if (*Values2 == NULL )
- ...
- /* Watch out by starting at subcript 1 rather than 0. */
- for (iter=1; iter<iDim; iter++)
- {
- (*Values1)[iter] = ...;
- (*Values2)[iter] = ...;
-
- }
-
- return 1;
- }
-
- Call from main:
-
- double * Ptr1;
- double * Ptr2;
-
- /* Why use the if you only return 1, it will always evaluate to true. */
-
- if (!GetValue( iSize, &Ptr1, &Ptr2 ) )....
-
- :)
- Jackson7@ix.netcom.com
-